using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NiSis.Entities;
using NiSis.Data;

namespace NiSis.UserControls
{
    public partial class MeseAnnoPicker : System.Web.UI.UserControl
    {
        #region 'MonthSelected' event definition code
        /// <summary>
        ///     EventArgs derived type which holds the custom event fields
        /// </summary>
        [Serializable]
        public class MonthSelectedEventArgs : System.EventArgs
        {
            /// <summary>
            ///     Use this constructor to initialize the event arguments
            ///     object with the custom event fields
            /// </summary>
            public MonthSelectedEventArgs(string cdMeseAnno)
            {
                _cdMeseAnno = cdMeseAnno;
            }

            private string _cdMeseAnno;

            public string CdMeseAnno
            {
                get { return _cdMeseAnno; }
                set { _cdMeseAnno = value; }
            }


        }

        /// <summary>
        ///     This represents the delegate method prototype that
        ///     event receivers must implement
        /// </summary>
        public delegate void MonthSelectedEventHandler(object sender, MonthSelectedEventArgs e);

        /// <summary>
        ///     TODO: Describe the purpose of MonthSelected here
        /// </summary>
        public event MonthSelectedEventHandler MonthSelected;

        /// <summary>
        ///     This is the method that is responsible for notifying
        ///     receivers that the event occurred
        /// </summary>
        protected virtual void OnMonthSelected(MonthSelectedEventArgs e)
        {
            if (MonthSelected != null)
            {
                MonthSelected(this, e);
            }
        }


        #endregion //('MonthSelected' event definition code)

        public bool SelectCommandVisible
        {
            get
            {
                return cmdSelect.Visible;
            }
            set
            {
                cmdSelect.Visible = value;
            }
        }
        private bool mSelectAll=false;

        public bool SelectAll
        {
            get { return mSelectAll; }
            set 
            { 
                mSelectAll = value;
                PopolateDdlMese();
            }
        }
	
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PopolateDdlMese();
                ddlMonth.SelectedIndex = 0;
            }
           
        }
        protected void PopolateDdlMese()
        {
            if (mSelectAll)
            {
                VList < VCdMeseAll > monthsAll= DataRepository.VCdMeseAllProvider.Get(string.Empty, "cd_mese_inizio_fornitura");
                ddlMonth.DataSource = monthsAll;
                ddlMonth.DataBind();
                return;
            }

            VList<VCdMese> months = DataRepository.VCdMeseProvider.Get(string.Empty, "cd_mese_inizio_fornitura");
            ddlMonth.DataSource = months;
            ddlMonth.DataBind();
        }
        //DateTime CurrentYear
        //{
        //    get
        //    {
        //        int iYear;
        //        int.TryParse(ddlMonth.SelectedValue.Substring());

        //        if (iYear == 0)
        //            return DateTime.Now;

        //        return DateTime.Parse(string.Format("01/01/{0}",cmbAnno.SelectedValue));

        //    }
        //}
        

        public string DateValue
        {
            get 
            {
                return ddlMonth.SelectedValue.ToString();
            }
            
        }
        [Obsolete("use DateValue instead")]
        public string DateValueOnCombo()
        {
            return ddlMonth.SelectedValue.ToString();
        }
        //public string DateTimeValue
        //{
        //    get
        //    {
        //        string sMonth = DateValueOnCombo().Remove(0, 4);
        //        return string.Format("{0}-{1}-{2}", CurrentYear.Year.ToString(), sMonth, "01");
        //    }

        //}
        
        public bool HasSelectedItem()
        {
            return ddlMonth.SelectedIndex > -1;
        }

        protected void cmdSelect_Click(object sender, ImageClickEventArgs e)
        {
            MonthSelectedEventArgs ea = new MonthSelectedEventArgs(DateValue);
            MonthSelected(this, ea);
        }
        
	
    }
}